﻿Imports System.Web
Imports System.Drawing
Imports System.Web.Services
Imports System.IO
Imports MeoTom.Core

Public Class Images
    Implements System.Web.IHttpHandler
    Public NewWidth As Integer = 0
    Public NewHeight As Integer = 0
    Public Lx As Integer = 0
    Public Ly As Integer = 0
    Public i As Integer

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "image/jpeg"
        Try
            Dim originalimg As System.Drawing.Image
            Dim crashImg As System.Drawing.Image

            Dim sFile As String = Configs.UserFolder.Replace("\Uploads", "") & context.Request.QueryString("sPath").Replace("/", "\")

            If Not File.Exists(sFile) Then
                crashImg = System.Drawing.Image.FromFile(Configs.UserFolder & "no_image_s.gif")
                crashImg.Save(context.Response.OutputStream, crashImg.RawFormat)
            Else
                originalimg = System.Drawing.Image.FromFile(sFile)

                If context.Request.QueryString("action") = "Article" Then
                    If context.Request.QueryString("mode") = "Thumbnail" Then
                        Lx = 130
                        Ly = 100
                    ElseIf context.Request.QueryString("mode") = "Medium" Then
                        Lx = 400
                        Ly = 280
                    ElseIf context.Request.QueryString("mode") = "Full" Then
                        Lx = originalimg.Width
                        Ly = originalimg.Height
                    End If
                ElseIf context.Request.QueryString("action") = "Product" Then
                    If context.Request.QueryString("mode") = "Thumbnail" Then
                        Lx = 126
                        Ly = 169
                    ElseIf context.Request.QueryString("mode") = "Thumbnail2" Then
                        Lx = 203
                        Ly = 272
                    ElseIf context.Request.QueryString("mode") = "Full" Then
                        Lx = originalimg.Width
                        Ly = originalimg.Height
                    End If
                End If

                If originalimg.Width < Lx Or originalimg.Height < Ly Then
                    NewHeight = originalimg.Height - ((originalimg.Height * 15) / 100)
                    NewWidth = originalimg.Width - ((originalimg.Width * 15) / 100)
                    i = 1
                Else
                    NewHeight = Ly
                    NewWidth = Lx
                    i = 0
                End If

                Dim thumb As New Bitmap(NewWidth, NewHeight)
                Dim gr_dest As Graphics = Graphics.FromImage(thumb)
                Dim sb = New SolidBrush(System.Drawing.Color.White)
                gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)
                gr_dest.DrawImage(originalimg, 0, 0, thumb.Width, thumb.Height)
                gr_dest.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                gr_dest.InterpolationMode = Drawing2D.InterpolationMode.High
                gr_dest.DrawString(Configs.WebName, New Font("Arial", 10), New SolidBrush(Color.Blue), (thumb.Width * 60) / 100, (thumb.Height * 1) / 100)

                If i = 1 Then
                    gr_dest.Save()
                    thumb.Save(context.Response.OutputStream, originalimg.RawFormat)
                Else
                    gr_dest.Save()
                    thumb.Save(context.Response.OutputStream, originalimg.RawFormat)
                End If

                If Not thumb Is Nothing Then
                    thumb.Dispose()
                    thumb = Nothing
                End If

                If Not originalimg Is Nothing Then
                    originalimg.Dispose()
                    originalimg = Nothing
                End If
            End If
        Catch ex As Exception
            'crashImg = System.Drawing.Image.FromFile(Configs.UserFolder & "no_image_s.gif")
            'crashImg.Save(context.Response.OutputStream, crashImg.RawFormat)
        Finally
            'originalimg = Nothing
            'crashImg = Nothing
        End Try
    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class